﻿
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Collections.Generic;
using System.Reflection;

using Job.Core;
using Job.Core.Common;
using Job.Core.Collections;

namespace Job.Core.Web
{
    /// <summary>
    /// Html 辅助操作封装。
    /// </summary>
    public class HtmlHelper
    {
        private static Dictionary<string, Dictionary<int, string>> EnumDict;

        /// <summary>
        /// 由枚举获得对应的词典 有缓存
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Dictionary<int, string> GetDictByEnum(Type type)
        {
            if (EnumDict == null)
            {
                EnumDict = new Dictionary<string, Dictionary<int, string>>();
            }
            Dictionary<int, string> dict;
            if (!EnumDict.ContainsKey(type.Name))
            {
                dict = Utils.GetEnumDictionary(type) as Dictionary<int, string>;
                EnumDict.Add(type.Name, dict);
            }
            else
            {
                dict = EnumDict[type.Name];
            }
            return dict;
        }

        /// <summary>
        /// 从枚举对应的词典中
        /// 获取枚举的index值的text
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetTxt(IDictionary<int, string> dict, string idStr)
        {
            StringBuilder sb = new StringBuilder();
            if (dict != null || string.IsNullOrEmpty(idStr))
            {
                string[] ids = idStr.Split(',');
                bool frist = true;
                foreach (var item in ids)
                {
                    if (string.IsNullOrEmpty(item))
                        continue;

                    string str = string.Empty;
                    int keyId = GetHelper.GetInt(item);
                    if (dict.ContainsKey(keyId))
                    {
                        str = dict[keyId];
                    }

                    if (string.IsNullOrEmpty(str))
                        continue;

                    if (frist)
                    {
                        frist = false;
                        sb.Append(str);
                    }
                    else
                    {
                        sb.AppendFormat(",{0}", str);
                    }
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 从枚举对应的词典中
        /// 获取枚举的index值的text
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetTxt(IDictionary<string, string> dict, string idStr)
        {
            StringBuilder sb = new StringBuilder();
            if (dict != null || string.IsNullOrEmpty(idStr))
            {
                string[] ids = idStr.Split(',');
                bool frist = true;
                foreach (var item in ids)
                {
                    if (string.IsNullOrEmpty(item))
                        continue;

                    string str = string.Empty;
                    if (dict.ContainsKey(item))
                    {
                        str = dict[item];
                    }

                    if (string.IsNullOrEmpty(str))
                        continue;

                    if (frist)
                    {
                        frist = false;
                        sb.Append(str);
                    }
                    else
                    {
                        sb.AppendFormat(",{0}", str);
                    }
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取一个为 checkbox 的 input 框。
        /// </summary>
        /// <param name="name">表单名称。</param>
        /// <param name="title">表单提示信息。</param>
        /// <param name="value">默认选择值。</param>
        /// <param name="attributes">其它 Html 特性。</param>
        /// <returns>表单 Html。</returns>
        public static string CheckBox(string name, string title, bool value, Dictionary<string, string> attributes)
        {
            return string.Format("<input type=\"checkbox\" name=\"{0}\" value=\"1\" id=\"{0}\"{1} /><label for=\"{0}\">{2}</label>", name,
                (value ? " checked=\"checked\"" : string.Empty) +
                (attributes == null || attributes.Count == 0 ? string.Empty : attributes.ForEach(keyValue => " " + keyValue.Key + "=\"" + keyValue.Value + "\"")),
                title);
        }

        /// <summary>
        /// 生成一个 select 表单。
        /// </summary>
        /// <param name="name">表单名称。</param>
        /// <param name="dataSource">要绑定到控件的信息源。</param>
        /// <param name="textField">绑定到控件显示的字段。</param>
        /// <param name="valueField">绑定到控件值的字段。</param>
        /// <param name="value">默认选择值。</param>
        /// <param name="attributes">其它 Html 特性。</param>
        /// <returns>表单 Html。</returns>
        public static string Select(string name, object dataSource, string textField, string valueField, string value, Dictionary<string, string> attributes)
        {
            return Select(name, dataSource, textField, valueField, new string[1] { value }, attributes, true);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dataSource"></param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="value"></param>
        /// <param name="attributes"></param>
        /// <param name="isZroe"></param>
        /// <returns></returns>
        public static string Select(string name, object dataSource, string textField, string valueField, string value, Dictionary<string, string> attributes, bool isZroe)
        {
            return Select(name, dataSource, textField, valueField, new string[1] { value }, attributes, isZroe);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dataSource"></param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string Select(string name, object dataSource, string textField, string valueField, string[] value, int type)
        {
            if (type == 1)
                return Select(name, dataSource, textField, valueField, value, null, true);
            else if (type == 2)
            {
                 Dictionary<string, string> dict=  new Dictionary<string, string>();
                 dict.Add("multiple", "multiple");
                 return Select(name, dataSource, textField, valueField, value, dict, true);
            }

            return Select(name, dataSource, textField, valueField, value, null, true);
        }

        /// <summary>
        /// 生成一个 select 表单。
        /// </summary>
        /// <param name="name">表单名称。</param>
        /// <param name="dataSource">要绑定到控件的信息源。</param>
        /// <param name="textField">绑定到控件显示的字段。</param>
        /// <param name="valueField">绑定到控件值的字段。</param>
        /// <param name="value">默认选择值 每个一项</param>
        /// <param name="attributes">其它 Html 特性。</param>
        /// <returns>表单 Html。</returns>
        public static string Select(string name, object dataSource, string textField, string valueField, List<string> value, Dictionary<string, string> attributes, bool isZroe)
        {
            return Select(name, dataSource, textField, valueField, value == null ? null : value.ToArray(), attributes, isZroe);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dataSource"></param>
        /// <param name="textField"></param>
        /// <param name="valueField"></param>
        /// <param name="value"></param>
        /// <param name="attributes"></param>
        /// <returns></returns>
        public static string Select(string name, object dataSource, string textField, string valueField, string[] value, Dictionary<string, string> attributes, bool isZroe)
        {
            Job.Core.Web.UI.Select sel = new Job.Core.Web.UI.Select();
            sel.ID = name;
            sel.DataSource = dataSource;
            sel.DataValueField = valueField;
            sel.DataTextField = textField;
            sel.DataBind();
            if(isZroe)
                sel.Items.Insert(0, new System.Web.UI.WebControls.ListItem("---", ""));

            if (attributes != null && attributes.Count > 0)
            {
                foreach (var keyValue in attributes)
                    sel.Attributes.Add(keyValue.Key, keyValue.Value);
            }
            if (value != null)
            {
                if (value.Length > 1)
                    sel.Multiple = true;

                foreach (var item in value)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        for (int i = 0; i < sel.Items.Count; i++)
                        {
                            if (sel.Items[i].Value == item)
                            {
                                sel.Items[i].Selected = true;
                                break;
                            }
                        }
                    }
                }
            }

            StringWriter sw = new StringWriter();
            Html32TextWriter htw = new Html32TextWriter(sw);
            sel.RenderControl(htw);
            return sw.ToString();
        }

        /// <summary>
        /// 获得 CheckBox 、 Radio 集
        /// </summary>
        /// <param name="name">Name</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="IsRadio">是否是 Radio</param>
        /// <param name="属性"></param>
        /// <returns></returns>
        public static string CheckBox(string name, Dictionary<int, string> dataSource, bool IsRadio, Dictionary<string, string> attributes)
        {
            if (dataSource == null)
            {
                return string.Empty;
            }
            string strAttributes = string.Empty;
            if (attributes != null && attributes.Count > 0)
            {
                foreach (var keyValue in attributes)
                {
                    strAttributes += string.Format(" \"{0}\"=\"{1}\" ", keyValue.Key, keyValue.Value);
                }
            }
            StringBuilder sb = new StringBuilder();
            int intVal = 1;
            foreach (var item in dataSource)
            {
                if (string.IsNullOrEmpty(item.Value))
                {
                    continue;
                }
                sb.AppendFormat("<input type=\"{4}\" name=\"{0}\" id=\"{0}_{1}\" value=\"{2}\" {5}/><label for=\"{0}_{1}\">{3}</label>"
                    , name
                    , intVal
                    , item.Key
                    , item.Value
                    , IsRadio ? "radio" : "checkbox"
                    , strAttributes
                    );
                intVal++;
            }

            return sb.ToString();
        }

        /// <summary>
        /// 获得 CheckBox 、 Radio 集
        /// </summary>
        /// <param name="name">Name</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="IsRadio">是否是 Radio</param>
        /// <param name="属性"></param>
        /// <returns></returns>
        public static string CheckBox(string name, Dictionary<string, string> dataSource, bool IsRadio, Dictionary<string, string> attributes)
        {
            if (dataSource == null)
            {
                return string.Empty;
            }
            string strAttributes = string.Empty;
            if (attributes != null && attributes.Count > 0)
            {
                foreach (var keyValue in attributes)
                {
                    strAttributes += string.Format(" \"{0}\"=\"{1}\" ", keyValue.Key, keyValue.Value);
                }
            }
            StringBuilder sb = new StringBuilder();
            int intVal = 1;
            foreach (var item in dataSource)
            {
                if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value))
                {
                    continue;
                }
                sb.AppendFormat("<input type=\"{4}\" name=\"{0}\" id=\"{0}_{1}\" value=\"{2}\" {5}/><label for=\"{0}_{1}\">{3}</label>"
                    , name
                    , intVal
                    , item.Key
                    , item.Value
                    , IsRadio ? "radio" : "checkbox"
                    , strAttributes
                    );
                intVal++;
            }

            return sb.ToString();
        }
      
    }
}
